package com.xunyun.kotlinwc.network.exception

import com.google.gson.JsonParseException
import org.json.JSONException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.text.ParseException

/**
 * @author gl
 * @date 2021/9/8
 * @desc
 */
object ExceptionUtil  {

    fun handleException(throwable: Throwable?): ApiException? {
        return if (throwable is JsonParseException
            || throwable is JSONException
            || throwable is ParseException
        ) {
            ApiException(ErrorCodeConstant.JSON_ERROR, "数据异常")
        } else if (throwable is ConnectException
            || throwable is UnknownHostException
            || throwable is SocketTimeoutException
        ) {
            ApiException(ErrorCodeConstant.NETWORK_TIMEOUT, "网路异常")
        } else {
            ApiException(ErrorCodeConstant.UNKNOWN_ERROR, "未知异常")
        }
    }
}